package com.tensquare.user.controller;

import cn.hutool.core.util.StrUtil;
import com.tensquare.user.pojo.User;
import com.tensquare.user.service.UserService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by WF on 2020/10/23 10:30
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;
    //1.获取手机验证码
    @PostMapping("/sendsms/{mobile}")
    public Result getCode(@PathVariable String mobile){
        userService.getCode(mobile);
        return new Result(StatusCode.OK,true,"发送验证码成功！");
    }

    //2.注册用户
    @PostMapping("register/{code}")
    public Result reg(@PathVariable String code,@RequestBody User user){
        //2.1)将code与redis中的进行比对
        boolean b = userService.isValidCode(user.getMobile(),code);
        //2.2)如果为true,可以注册
        if(b){
            userService.add(user);
            return new Result(StatusCode.OK,true,"注册成功！");
        }
        return new Result(StatusCode.ERROR,false,"注册失败！");
    }

    //3.添加用户
    @PostMapping
    public Result add(@RequestBody User user){
        userService.add(user);
        return new Result(StatusCode.OK,true,"添加用户成功！");
    }

    //4.用户登录
    @PostMapping("login")
    public Result login(@RequestBody User user){
        //4.1)进行登录认证
        User loginUser = userService.login(user);
        //4.2)创建jwt
        String token = jwtUtil.createJWT(loginUser.getId(), loginUser.getNickname(), "user");

        //4.3)是否登录成功
        if(loginUser != null){
            Map map = new HashMap<>();
            map.put("name",loginUser.getNickname());
            map.put("token",token);
            return new Result(StatusCode.OK,true,"登录成功！",token);
        }
        return new Result(StatusCode.LOGINERROR,false,"登录失败！");
    }

    //5.删除用户
    @DeleteMapping("{userId}")
    public Result delete(@PathVariable String userId){
        //5.1)得到请求作用域中的角色名
        Claims claims = (Claims) request.getAttribute("claims");
        //5.2)判断是否存在
        if(claims != null){
            //5.3)如果存在，就得到角色
            String roles = (String) claims.get("roles");
            //5.4)判断角色是否存在
            if(StrUtil.isNotBlank(roles) && "admin".equals(roles)){
                userService.delete(userId);
                return new Result(StatusCode.OK,true,"删除用户成功！");
            }
        }
        return new Result(StatusCode.ACCESSERROR,false,"无权删除用户！");
    }

    //6.增加粉丝数
    @PostMapping("incfans/{friendid}/{n}")
    public void incfans(@PathVariable String friendid ,@PathVariable int n){
        userService.incfans(friendid,n);
    }

    //7.增加关注数
    @PostMapping("incfollow/{userid}/{n}")
    public void incfollow(@PathVariable String userid ,@PathVariable int n){
        userService.incfollow(userid,n);
    }
}
